package com.ehualu.getes.until;

import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.client.RestHighLevelClient;
import org.elasticsearch.index.query.BoolQueryBuilder;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.search.builder.SearchSourceBuilder;
import org.elasticsearch.search.sort.SortOrder;

import java.io.IOException;
import java.util.*;

/**
 * @Author: 吴敬超
 * @Date: 2019/11/22 11:24
 */
public class ESguaUntil {

    /*
     *得到指定车牌的最近10条过车数据（卡口去重）
     * */
    public List<String[]> getguokkidtop(String index, String carnum, RestHighLevelClient client) {

        List<String[]> listkkinfo = new ArrayList<>();
        List<String> flaglist = new ArrayList<>();
        try {

            String indexName = index;

            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//			sourceBuilder.from(0); // 默认0
//            sourceBuilder.size(10000);// 默认10条
            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("ccarnumber.keyword", carnum));
//                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));
            sourceBuilder.sort("createtime", SortOrder.DESC);

            sourceBuilder.query(queryBuilder);
            searchRequest.source(sourceBuilder);
            SearchResponse response = client.search(searchRequest);
            SearchHits hits = response.getHits();
            SearchHit[] searchHits = hits.getHits();
//            int sl = searchHits.length;
//            System.out.println("查询数据量：" + sl);
            for (SearchHit hit : searchHits) {

                Map<String, Object> sourceAsMap = hit.getSourceAsMap();// 获取查询结果json

                String strkkid = (String) sourceAsMap.get("caddresscode");
                String strkkmc = (String) sourceAsMap.get("ccollectionaddress");
                String strgcsj = (String) sourceAsMap.get("createtime");
                String strjd = (String) sourceAsMap.get("jd");

                String strwd = (String) sourceAsMap.get("wd");


                System.out.println("strkkid:" + strkkid);
                System.out.println("strkkmc:" + strkkmc);
                System.out.println("strgcsj:" + strgcsj);

                System.out.println("strjd:" + strjd);

                System.out.println("strwd:" + strwd);
                String szgcinfo[] = new String[5];

                szgcinfo[0] = strkkid;
                szgcinfo[1] = strkkmc;
                szgcinfo[2] = strgcsj;
                szgcinfo[3] = strjd;
                szgcinfo[4] = strwd;

                if (flaglist.contains(strkkid)) {

                    System.out.println("该卡口已经记录过了");
                } else {

                    flaglist.add(strkkid);
                    listkkinfo.add(szgcinfo);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        return listkkinfo;
    }


    /*
     *得到指定车牌的最近N条过车数据（卡口去重，返回SearchHit[]）
     * */
    public SearchHit[] getguokkidtopSearchHit(String index, String carnum, Integer llnum, RestHighLevelClient client) {


        SearchHit[] searchHits = null;
        try {

            String indexName = index;

            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//			sourceBuilder.from(0); // 默认0
            sourceBuilder.size(llnum);// 默认10条
            QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("ccarnumber.keyword", carnum));
//                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));
            sourceBuilder.sort("createtime", SortOrder.DESC);

            sourceBuilder.query(queryBuilder);
            searchRequest.source(sourceBuilder);
            SearchResponse response = client.search(searchRequest);
            SearchHits hits = response.getHits();
            searchHits = hits.getHits();


        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return searchHits;
    }


    /*
     *指定卡口，指定时间段，取车牌，用了 set 去重
     * */
    public Map<String, String> getcpfortimeandkkid(String carnumtype, String starttime, String endTimeStr, String index, String[] kkid, String hpys, RestHighLevelClient client) {


        Map<String, String> carnummap = new HashMap<>();


        try {

            String indexName = index;

            SearchRequest searchRequest = new SearchRequest(indexName);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//			sourceBuilder.from(0); // 默认0
//            sourceBuilder.size(10000);// 默认10条

            QueryBuilder queryBuilder = null;
            BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("hpys.keyword", hpys))
                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));

            if (kkid.length == 1) {

                System.out.println("匹配的卡口只一个");
                boolQueryBuilder.must(QueryBuilders.termQuery("caddresscode.keyword", kkid[0]));

            } else {

                boolQueryBuilder.must(QueryBuilders.termsQuery("caddresscode.keyword", kkid));

            }

           /* QueryBuilder queryBuilder = QueryBuilders.boolQuery()
                    .must(QueryBuilders.termQuery("caddresscode.keyword", kkid))
                    .must(QueryBuilders.rangeQuery("createtime").from(starttime).to(endTimeStr));*/
            queryBuilder = boolQueryBuilder;

            sourceBuilder.query(queryBuilder);
            searchRequest.source(sourceBuilder);
            SearchResponse response = client.search(searchRequest);
            SearchHits hits = response.getHits();
            SearchHit[] searchHits = hits.getHits();

            for (SearchHit hit : searchHits) {


                Map<String, Object> sourceAsMap = hit.getSourceAsMap();// 获取查询结果json

                String strccarnumber = (String) sourceAsMap.get("ccarnumber");
                String strpic1 = (String) sourceAsMap.get("pic1");

                System.out.println("strccarnumber:" + strccarnumber);
                System.out.println("strpic1:" + strpic1);

                if (carnumtype.equals("w")) {

                    /*
                     * 根据车尾找车头
                     * 车头不会含有 挂 学 字
                     * */

                    if (strccarnumber.contains("挂")) {
                        System.out.println("车牌包含 挂 字，舍弃");

                    } else if (strccarnumber.contains("学")) {
                        System.out.println("车牌包含 学 字，舍弃");

                    } else {

                        carnummap.put(strccarnumber, strpic1);
                    }

                } else {

                    /*
                     * 根据车头找车尾
                     * 找车尾为挂的
                     * */
                    if (strccarnumber.contains("挂")) {

                        carnummap.put(strccarnumber, strpic1);

                    }

                }

            }

        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e1) {
            e1.printStackTrace();
        }
        return carnummap;
    }

}
